package com.xjk.encryp;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author 许嘉凯
 */
public class MD5Util {


  /**
   * MD5加密算法1
   * @param input
   * @return
   */
  public static String MD5Hash1(String input) {
    try {
      MessageDigest md = MessageDigest.getInstance("MD5");
      byte[] messageDigest = md.digest(input.getBytes());

      StringBuilder hexString = new StringBuilder();
      for (byte b : messageDigest) {
        String hex = Integer.toHexString(0xff & b);
        if (hex.length() == 1) {
          hexString.append('0');
        }
        hexString.append(hex);
      }
      return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
      return null;
    }
  }

  /**
   * MD5加密算法2
   * @param data
   * @return
   */
  public static byte[] encryMD5(byte[] data) throws Exception {
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    md5.update(data);
    return md5.digest();
  }


}
